package com.tykboot.gem;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author LiuYu
 * @date 2017/12/5
 */
public class SwaggerWebGen {
    /**
     * 功能：生成Dao类主体代码
     */
    public static void parse(String tableComment, String entityName, String priType) {
        String webName = entityName + "Controller";
        StringBuffer sb = new StringBuffer();
        sb.append("package " + GenTool.webPackageOutPath + ";\r\n");
        sb.append("\r\n");

//        sb.append("import com.alibaba.fastjson.JSONObject;\r\n");
        sb.append("import com.tykboot.core.base.BaseController;\r\n");
        sb.append("import " + GenTool.entityPackageOutPath + "." + entityName + ";\r\n");
        sb.append("import com.tykboot.message.ResponseMsg;\r\n");
        sb.append("import " + GenTool.svcPackageOutPath + ".I" + entityName + "Service;\r\n");
        sb.append("import io.swagger.annotations.Api;\r\n");
        sb.append("import io.swagger.annotations.ApiImplicitParam;\r\n");
        sb.append("import io.swagger.annotations.ApiOperation;\r\n");
        sb.append("import org.springframework.beans.factory.annotation.Autowired;\r\n");
//        sb.append("import org.springframework.data.domain.Page;\r\n");
//        sb.append("import org.springframework.data.domain.PageRequest;\r\n");
//        sb.append("import org.springframework.data.domain.Pageable;\r\n");
        sb.append("import org.springframework.web.bind.annotation.*;\r\n\r\n");
        sb.append("import java.util.List;\r\n\r\n");

        //注释部分
        sb.append("/**\r\n");
        sb.append(" * " + tableComment + " Controller\r\n");
        sb.append(" *\r\n");
        sb.append(" * @author " + GenTool.authorName + "\r\n");
        sb.append(" */ \r\n");

        sb.append("@Api(tags = \"" + tableComment + "\")\r\n");
        sb.append("@RestController\r\n");
        sb.append("@RequestMapping(BaseController.API + \"/" + GenTool.initcap2(entityName) + "\")\r\n");

        //实体部分
        sb.append("public class " + webName + " extends BaseController {\r\n\r\n");
        sb.append("\t@Autowired\r\n");
        sb.append("\tprivate I" + entityName + "Service " + GenTool.initcap2(entityName) + "Service;\r\n\r\n");

        //新增
        sb.append("\t@ApiOperation(value = \"添加\", notes = \"添加\")\r\n");
        sb.append("\t@ApiImplicitParam(paramType = \"body\", dataType = \"" + entityName + "\", name = \"model\", value = \"实体\", required = true)\r\n");
        sb.append("\t@PostMapping(\"/add\")\r\n");
        sb.append("\tpublic ResponseMsg<" + entityName + "> add(@RequestBody " + entityName + " requestBody) {\r\n");
        sb.append("\t\tResponseMsg<" + entityName + "> responseMsg = new ResponseMsg<>();\r\n");
        sb.append("\t\t" + entityName + " result = " + GenTool.initcap2(entityName) + "Service.save(requestBody);\r\n");
        sb.append("\t\tresponseMsg.setData(result);\r\n");
        sb.append("\t\treturn responseMsg;\r\n");
        sb.append("\t}\r\n\r\n");

        //删除
        sb.append("\t@ApiOperation(value = \"删除\", notes = \"删除\")\r\n");
        sb.append("\t@DeleteMapping(\"/deleteById/{id}\")\r\n");
        sb.append("\tpublic ResponseMsg deleteById(@PathVariable " + priType + " id) {\r\n");
        sb.append("\t\tResponseMsg responseMsg = new ResponseMsg();\r\n");
        sb.append("\t\t" + GenTool.initcap2(entityName) + "Service.deleteById(id);\r\n");
        sb.append("\t\treturn responseMsg;\r\n");
        sb.append("\t}\r\n\r\n");

        //查询全部
        sb.append("\t@ApiOperation(value = \"查询全部\", notes = \"查询全部\")\r\n");
        sb.append("\t@GetMapping(\"/queryAll\")\r\n");
        sb.append("\tpublic ResponseMsg<List<" + entityName + ">> queryAll() {\r\n");
        sb.append("\t\tResponseMsg<List<" + entityName + ">> responseMsg = new ResponseMsg<>();\r\n");
        sb.append("\t\tresponseMsg.setData(" + GenTool.initcap2(entityName) + "Service.findAll());\r\n");
        sb.append("\t\treturn responseMsg;\r\n");
        sb.append("\t}\r\n\r\n");

        //查询单个
        sb.append("\t@ApiOperation(value = \"查询单个\", notes = \"查询单个\")\r\n");
        sb.append("\t@GetMapping(\"/queryById/{id}\")\r\n");
        sb.append("\tpublic ResponseMsg<" + entityName + "> queryById(@PathVariable " + priType + " id) {\r\n");
        sb.append("\t\tResponseMsg<" + entityName + "> responseMsg = new ResponseMsg<>();\r\n");
        sb.append("\t\tresponseMsg.setData(" + GenTool.initcap2(entityName) + "Service.findById(id));\r\n");
        sb.append("\t\treturn responseMsg;\r\n");
        sb.append("\t}\r\n\r\n");

        //修改
        sb.append("\t@ApiOperation(value = \"修改\", notes = \"修改\")\r\n");
        sb.append("\t@ApiImplicitParam(paramType = \"body\", dataType = \"" + entityName + "\", name = \"model\", value = \"实体\", required = true)\r\n");
        sb.append("\t@PutMapping(\"/edit\")\r\n");
        sb.append("\tpublic ResponseMsg<" + entityName + "> edit(@RequestBody " + entityName + " requestBody) {\r\n");
        sb.append("\t\tResponseMsg<" + entityName + "> responseMsg = new ResponseMsg<>();\r\n");
        sb.append("\t\t" + entityName + " result = " + GenTool.initcap2(entityName) + "Service.save(requestBody);\r\n");
        sb.append("\t\tresponseMsg.setData(result);\r\n");
        sb.append("\t\treturn responseMsg;\r\n");
        sb.append("\t}\r\n\r\n");
        sb.append("}");

        try {
            String path = GenTool.class.getClass().getResource("/").getPath().split("/target")[0];
            String outputPath = path + "/src/main/java/" + GenTool.webPackageOutPath.replace(".", "/");
            File outPutFile = new File(outputPath);
            if (!outPutFile.exists()) {
                outPutFile.mkdirs();
            }
            String filePath = outputPath + "/" + webName + ".java";
            FileWriter fw = new FileWriter(filePath);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(sb.toString());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("生成Controller: " + webName + ".java");
    }
}
